package cn.yitun.chat.yitunserverimapicore.business.group.service.impl;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.Info;
import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupJoinVerifyQuestionDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupSettingDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.group.GroupSettingInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupJoinVerifyQuestionVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupSettingVo;
import cn.yitun.chat.yitunservercommonlib.enums.groupChat.GroupJoinVerifySettingTypeEnums;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupJoinSettingService;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupJoinVerifyQuestionDao;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupSettingDao;
import cn.yitun.chat.yitunserverimcore.general.main.biz.group.manager.GroupJoinSettingManager;
import cn.yitun.chat.yitunserverimcore.general.main.biz.group.manager.GroupMemberManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Date 2019-01-20 18:21:27<br>
 * Description
 * 
 * @author yitun
 * @since 1.0.0
 */
@Service
@Transactional
public class GroupJoinSettingServiceImpl
		extends CrudServiceImpl<GroupSettingDao, GroupSettingDto, GroupSettingInfo, GroupSettingVo>
		implements GroupJoinSettingService {

	@Resource
	private GroupJoinVerifyQuestionDao groupJoinVerifyQuestionDAO;
	@Resource
	private GroupJoinSettingManager groupJoinSettingManager;
	@Resource
	private GroupMemberManager groupMemberManager;

	public GroupSettingVo getByGroupId(Long groupId) {
		GroupSettingVo data = groupJoinSettingManager.getByGroupId(groupId);
		return data;
	}

	public List<GroupJoinVerifyQuestionVo> getVerifyQuestionListByGroupId(Long groupId) {
		List<GroupJoinVerifyQuestionVo> list = groupJoinVerifyQuestionDAO.listVO(
				R.param("groupId", groupId));
		return list;
	}

	public Info saveOrUpdate(Long ownerUserId, GroupSettingDto data) {
		List<GroupJoinVerifyQuestionDto> list = data.getGroupJoinVerifyQuestionList();
		Info info = new Info();
		try {
			Long groupId = data.getGroupId();
			boolean isOwner = groupMemberManager.isOwner(groupId, ownerUserId);
			if (!isOwner) {
				info.addWarning(ExceptionEnum.SOCK_GROUP_SETTING_NO_PERMISSION);
			}

			if (GroupJoinVerifySettingTypeEnums.VERIFY_TYPE_ANSWER.getType().equals(data.getJoinType())) {
				if (YitunStringUtil.isEmpty(data.getQuestion())) {
					info.addWarning(ExceptionEnum.SOCK_CONTACT_QUESTION_CANT_BE_NULL);
				}
				if (YitunStringUtil.isEmpty(data.getAnswer())) {
					info.addWarning(ExceptionEnum.SOCK_CONTACT_ANSWER_CANT_BE_NULL);
				}
			}
			if (GroupJoinVerifySettingTypeEnums.VERIFY_TYPE_CONFIRM.getType().equals(data.getJoinType())) {
				if (YitunCollectionUtil.isEmpty(list)) {
					info.addWarning(ExceptionEnum.SOCK_CONTACT_QUESTION_CANT_BE_NULL);
				} else {
					int i = 1;
					for (GroupJoinVerifyQuestionDto d : list) {
						if (YitunStringUtil.isEmpty(d.getQuestion())) {
							info.addWarning(ExceptionEnum.SOCK_CONTACT_QUESTION_CANT_BE_NULL);
						}
						i++;
					}
				}
			}
			if (info.isSuccess()) {
				saveOrUpdate(data);
				saveOrUpdate(data.getGroupId(), list);
			}
		} catch (Exception e) {
			info.addWarning(ExceptionEnum.BUSY_SERVER);
		}

		return info;
	}

	public void saveOrUpdate(GroupSettingDto data) {
		Long groupId = data.getGroupId();
		GroupSettingVo groupSettingVo = dao.getVOByGroupId(groupId);
		if (null == groupSettingVo) {
			dao.save(data);
		} else {
			data.setId(groupSettingVo.getId());
			dao.update(data);
		}
	}

	public void saveOrUpdate(Long groupId, List<GroupJoinVerifyQuestionDto> list) {
		groupJoinVerifyQuestionDAO.deleteByGroupId(groupId);
		for (GroupJoinVerifyQuestionDto data : list) {
			data.setGroupId(groupId);
			groupJoinVerifyQuestionDAO.save(data);
		}
	}
}
